package com.securitydemo.securitydemo.config;


import com.securitydemo.securitydemo.handler.JwtAuthorizationTokenFilter;
import com.securitydemo.securitydemo.handler.RestAuthorizationEntryPoint;
import com.securitydemo.securitydemo.handler.RestfulAccessDeniedHandler;
import com.securitydemo.securitydemo.pojo.Admin;
import com.securitydemo.securitydemo.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private IAdminService adminService;
   @Autowired
   private RestfulAccessDeniedHandler restfulAccessDeniedHandler;
   @Autowired
   private RestAuthorizationEntryPoint restAuthorizationEntryPoint;

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/login",
                "/logout",
                "/css/**",
                "/js/**",
                "/index.html",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources/**",
                "/v2/api-docs/**",
                "/captcha"
        );
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //基于jwt不需要session
        http.csrf().disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                //禁用缓存
                .headers()
                .cacheControl();
        //添加jwt登录授权过滤器
       http.addFilterBefore(jwtAuthorizationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
        //添加自定义未登录结果未授权结果自定义返回
        http.exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler) //未登录拦截
                .authenticationEntryPoint(restAuthorizationEntryPoint); //没有权限拦截
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }



    @Bean
    public UserDetailsService userDetailsService(){
    return username->{
        Admin admin = adminService.getAdminByUsername(username);
        if(null!=admin){
            return admin;
        }
        return null;
      };
   }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
   }
    @Bean
    public JwtAuthorizationTokenFilter jwtAuthorizationTokenFilter(){
        return new JwtAuthorizationTokenFilter();
   }
}


